
package com.yundingai.controller;

import com.yundingai.domain.entity.AllPlan;
import com.yundingai.service.AllPlanService;
import com.yundingai.service.RelUserPlanService;
import com.yundingai.vm.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/plans")
@CrossOrigin //处理跨域
public class AllPlanController {

    @Autowired
    AllPlanService allPlanService;



    /**
     * 查询计划
     * @return
     */
    @GetMapping("/user/{userId}")
    public ResponseResult<List<AllPlan>> selectPlansByUserId(@PathVariable Integer userId){
        List<AllPlan> allPlans =allPlanService.selectPlansByUserId(userId);
        return ResponseResult.okResult(allPlans);
    }
    /**
     * 更新计划
     */
    @PutMapping()
    public ResponseResult updatePlan(@RequestBody AllPlan allPlan){
        allPlanService.updatePlan(allPlan);
        return ResponseResult.okResult();
    }
    /**
     * 删除计划
     */
    @DeleteMapping("/{planId}")
    public ResponseResult deletePlan(@PathVariable Integer planId){
        allPlanService.deletePlan(planId);
        return ResponseResult.okResult();
    }
    /**
     * 新增计划
     */
    @PostMapping("/{userId}")
    public ResponseResult addPlan(@RequestBody AllPlan allPlan,@PathVariable Integer userId) {
        allPlanService.addPlan(allPlan,userId);
        return ResponseResult.okResult();
    }
    /**
     * 获取单个计划
     */
    @GetMapping("/{planId}")
    public ResponseResult selectPlanById(@PathVariable Integer planId){
        AllPlan allPlan = allPlanService.selectPlanById(planId);
        return ResponseResult.okResult(allPlan);
    }
}